package makovo.controller;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

import makovo.utils.DataAccess;
import makovo.data.User_Register;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class ValidatorRegister extends ActionSupport implements SessionAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String mTextPattern = "^[a-zA-Z0-9áéíóúÁÉÍÓÚàèìòùÀÈÌÒÙñÑ \\-.]{1,}$";
	private String mEmailPattern = "^[a-zA-Z0-9.]{1,}[@]{1}[a-zA-Z0-9.]{1,}$";
	private String mNumberPatter = "^[0-9.,]{1,}$";
	private String mOnlyNumberPatter = "^[0-9]{1,}$";
	private String mDatePattern = "^[0-9]{4}/[0-9]{2}/[0-9]{2}$";

	private String mName;
	private String mLastName;
	private String mSecondLastName;
	private String mStateID;
	private String mState;
	private String mPhone;

	public String getName() {
		return mName;
	}

	public void setName(String name) {
		try {
			mName = new String(name.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getLastName() {
		return this.mLastName;
	}

	public void setLastName(String lastName) {
		try {
			mLastName = new String(lastName.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getSecondLastName() {
		return this.mSecondLastName;
	}

	public void setSecondLastName(String secondLastName) {
		try {
			mSecondLastName = new String(secondLastName.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getStateId() {
		return mStateID;
	}

	public void setStateId(String stateId) {
		mStateID = stateId;
	}

	public String getState() {
		return mState;
	}

	public void setState(String state) {
		try {
			mState = new String(state.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getPhone() {
		return mPhone;
	}

	public void setPhone(String phone) {
		mPhone = phone;
	}

	public void validate() {

		if (!mName.matches(mTextPattern))
			addActionError("Nombre del usuario");

		if (!mLastName.matches(mTextPattern))
			addActionError("Apellido paterno del usuario");

		if (!mSecondLastName.matches(mTextPattern))
			addActionError("Apellido materno del aplicante");

		if (Integer.parseInt(mStateID) == -1)
			addActionError("Estado");

	}

	public String execute() {
		String accion = "ERROR";

		if (User_Register.registerValidator(mName, mLastName, mSecondLastName, mStateID, mState, mPhone)) {
			accion = "SUCCESS";
		}
		return accion;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub

	}
}

// sp_registro_validador('Efren','Cruz','Carlin',10,'Mex','56100101');